package com.inspinia.message.common.model;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * spring加载后改方法的子类
 * @author Veblen
 * */
public abstract class AbstractAfterSpringBegin  implements ApplicationListener<ContextRefreshedEvent>,Runnable {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // TODO Auto-generated method stub
        if (event.getApplicationContext().getParent() == null) {
            //org.apache.commons.lang3.concurrent.BasicThreadFactory
            ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
                    new BasicThreadFactory.Builder().namingPattern("websocketServer-schedule-pool-%d").daemon(true).build());
            executorService.schedule(this,0,TimeUnit.MICROSECONDS);
        }
    }
}
